$("#dochange").click(function() {
    changeLogPwd.doFind();
});
// 获取短信
$("#getCode").click(function() {
    changeLogPwd.sendCode();
});
var changeLogPwd = {
    doFind: function() {
        this.password = $("#password").val();
        this.password1 = $("#password1").val();
        this.smscode = $("#code").val();
        this.check();
        if (this.flog) {
            $.post(
                "/index.php/Changepassword/editChangeLoginPwd", {
                    password: this.password,
                    smscode: this.smscode,
                },
                function(reg) {
                    if (reg.status == "success") {
                        layer.msg(reg.data, {
                            icon: 1
                        })
                        setTimeout(function() {
                            window.location.href = "/index.php/Login";
                        }, 1500);

                    } else if (reg.status == "failed") {
                    	if(reg.data == "您还没有登录，请先登录！"){
                    		layer.msg(reg.data, {
	                            icon: 2
	                        })
	                        setTimeout(function() {
	                            window.location.href = "/index.php/Login";
	                        }, 1500);
                    	}else{
                    		layer.msg(reg.data, {
	                            icon: 2
	                        })
                    	}
                    }
                }
            );
        }
    },
    sendCode: function() {
        var that = this;
        $.get(
            "/changepassword/getSmsCode",
            function(reg) {
                if (reg.status == "success") {
                    layer.msg(reg.data, {
                        icon: 1
                    })
                    that.disableButton();
                } else if (reg.status == "failed") {
                    //that.isStartCheckCode = true;//------------
                    layer.msg(reg.data, {
                        icon: 2
                    })
                }
            }
        );
    },
    check: function() {
        //提交给服务器前的验证
        if (this.password.length < 6) {
            layer.tips("密码不能少于6位", "#password");
            $("#password").focus();
        } else if (this.password != this.password1) {
            layer.tips("两次输入密码不一致", "#password1");
            $("#password1").focus();
        } else {
        //     else if (this.smscode.length == 0) {
        //     layer.tips("验证码不能为空", "#code");
        //     $("#code").focus();
        // } 
            this.flog = true;
        }
    },
    disableButton: function() {
        $("#getCode").attr('disabled', "disabled");
        var timestamp = new Date().getTime(); //得到当前时间戳  毫秒级别
        localStorage.codeTime1 = timestamp;
        this.recoveryButton();
    },
    recoveryButton: function(ts) {
        var that = this;
        var timestamp1 = new Date().getTime();
        ts = timestamp1 - parseInt(localStorage.codeTime1);
        $("#getCode").attr('disabled', "disabled");
        if (ts < 60000) {
            $("#getCode").text(60 - parseInt(ts / 1000) + "S");
            setTimeout(function() {
                --ts;
                that.recoveryButton(ts);
            }, 1000);

        } else {
            $("#getCode").removeAttr('disabled');
            $("#getCode").blur();
            $("#getCode").text("获取短信验证码");
        }
    }
};

changeLogPwd.recoveryButton();
